package com.ddwl.user.controller;

import com.ddwl.common.annotation.Auth;
import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.controller.BaseController;
import com.ddwl.schema.bo.base.PageBo;
import com.ddwl.schema.bo.user.AccountBo;
import com.ddwl.schema.bo.user.AccountCreateBo;
import com.ddwl.schema.bo.user.AccountQueryBo;
import com.ddwl.schema.bo.user.DictionaryListBo;
import com.ddwl.schema.vo.base.PageVo;
import com.ddwl.schema.vo.base.RestVo;
import com.ddwl.schema.vo.user.AccountVo;
import com.ddwl.schema.vo.user.DictionaryListVo;
import com.ddwl.schema.vo.user.MenuVo;
import com.ddwl.user.service.AccountService;
import com.ddwl.user.service.MenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author Fu Zhaohui
 * @version 1.0.0
 * @date 2019/8/12 18:50
 */
@Api(tags = "商户帐号服务")
@RequestMapping("/account")
@RestController
@RequiredArgsConstructor
public class AccountController extends BaseController {

    private final AccountService accountService;
    private final MenuService menuService;

    @PostMapping("/check")
    @ApiOperation("检查商户登录帐号")
    @Auth(product = BaseEnum.ProductEnum.CONSOLE)
    public RestVo<Boolean> check(@RequestParam String username) {
        return RestVo.ok(accountService.check(username));
    }


    @PostMapping("/create")
    @ApiOperation("创建管理帐号")
    @Auth(product = BaseEnum.ProductEnum.CONSOLE)
    public RestVo<String> create(@Valid @RequestBody AccountCreateBo bo) {
        return RestVo.ok(accountService.create(bo));
    }

    @PostMapping("/update")
    @ApiOperation("修改管理帐号")
    @Auth(product = BaseEnum.ProductEnum.CONSOLE)
    public RestVo<AccountVo> update(@Valid @RequestBody AccountBo bo) {
        fillBaseField(bo);
        accountService.update(bo);
        return RestVo.ok();
    }

    @PostMapping("/forbid")
    @ApiOperation("封禁管理帐号")
    @Auth(product = BaseEnum.ProductEnum.CONSOLE)
    public RestVo<AccountVo> forbid(@RequestBody AccountBo bo) {
        fillBaseField(bo);
        accountService.forbid(bo);
        return RestVo.ok();
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除管理帐号")
    @Auth(product = BaseEnum.ProductEnum.CONSOLE)
    public RestVo<AccountVo> delete(@RequestParam String id) {
        accountService.delete(id);
        return RestVo.ok();
    }

    @GetMapping("/info")
    @ApiOperation("获取帐户信息")
    @Auth(product = BaseEnum.ProductEnum.CONSOLE)
    public RestVo<AccountVo> getProfile() {
        return RestVo.ok(accountService.getById(getUserId()));
    }


    @PostMapping("/page")
    @ApiOperation("分页查询帐户")
    @Auth(product = BaseEnum.ProductEnum.CONSOLE)
    public RestVo<PageVo<AccountVo>> page(@Valid @RequestBody PageBo<AccountQueryBo> pageBo) {
        return RestVo.ok(accountService.page(pageBo));
    }

    @Deprecated
    @PostMapping("/queryDictionaryList")
    @ApiOperation("查询字典数据(已废弃)")
    public RestVo<DictionaryListVo> queryDictionaryList(@Valid @RequestBody DictionaryListBo bo) {
        DictionaryListVo vo = new DictionaryListVo();
        vo.setCustomerServiceTel("19157818805");
        return RestVo.ok(vo);
    }

    @ApiOperation("查询用户的菜单")
    @GetMapping("/menu")
    @Auth(product = BaseEnum.ProductEnum.CONSOLE)
    public RestVo<List<MenuVo>> getUserMenus() {
        return RestVo.ok(menuService.getUserMenus(getUserId()));
    }

    @PostMapping("/changeId")
    @ApiOperation("把id反转成时间戳")
    @Auth(product = BaseEnum.ProductEnum.CONSOLE)
    public RestVo changeId() {
        accountService.changeId();
        return RestVo.ok();
    }


}
